#include "../include/filehandle.h"

extern byte codes[0x1000000];
extern byte datas[0x1000000];

std::vector<pHeader> headers;

fHeader getHeader(const char *filename)
{
    std::ifstream inFile(filename, std::ios::binary);
    fHeader result;
    inFile.read((char *)&result, sizeof(fHeader));
    if (strcmp(result.magic, "sandbox") != 0)
    {
        std::cout << "fatal error:file type error.\n";
        exit(1);
    }
    else if (result.version != 0x0203)
    {
        std::cout << "fatal error:file version error.\n";
        exit(1);
    }
    return result;
}
void getPara(const char *filename, fHeader fMessage)
{
    std::ifstream inFile(filename, std::ios::binary);
    inFile.seekg(fMessage.fEntry);
    for (int i = 0; i < fMessage.pCount; i++)
    {
        pHeader pMessage;
        inFile.seekg(fMessage.fEntry + i * 8);
        inFile.read((char *)&pMessage, sizeof(pHeader));
        headers.push_back(pMessage);
    }
}
void loadImage(const char *filename)
{
    std::ifstream inFile(filename, std::ios::binary);
    for (auto &I : headers)
    {
        if (I.checkNum == 0xff)
            return;
        inFile.seekg(I.fBegin);
        switch (I.pType)
        {
        case 0x00:
            inFile.read((char *)codes + I.pEntry, I.pSize);
            break;
        case 0x01:
            inFile.read((char *)datas + I.pEntry, I.pSize);
            break;
        }
    }
}